package com.wyh.yuhcloud.config;


import com.wyh.yuhcloud.filter.TokenAuthFilter;
import com.wyh.yuhcloud.util.DefaultPasswordEncoder;
import com.wyh.yuhcloud.util.TokenLogoutHandler;
import com.wyh.yuhcloud.util.TokenManager;
import com.wyh.yuhcloud.util.UnAuthEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author wyh
 * @version 1.0
 * @date 2021/10/5 16:24
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private TokenManager tokenManager;
    private RedisTemplate redisTemplate;
    private DefaultPasswordEncoder defaultPasswordEncoder;
    private UserDetailsService userDetailsService;

    @Autowired
    public TokenWebSecurityConfig(UserDetailsService userDetailsService, DefaultPasswordEncoder defaultPasswordEncoder,
                                  RedisTemplate redisTemplate, TokenManager tokenManager){
        this.defaultPasswordEncoder=defaultPasswordEncoder;
        this.userDetailsService=userDetailsService;
        this.redisTemplate=redisTemplate;
        this.tokenManager=tokenManager;

    }

    //配置http组件
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnAuthEntryPoint()) //设置未授权管理
                .and().csrf().disable()//关闭csrf
                .authorizeRequests()
                .anyRequest().authenticated() //拦截所有请求
                .and()
                .formLogin()
                .loginPage("/toLogin") // 设置登录页面
                .successForwardUrl("/index")
                .and().logout().logoutUrl("/user/logout")//设置退出路径
                .addLogoutHandler(new TokenLogoutHandler(tokenManager,redisTemplate))//添加退出处理程序
                //.and().addFilter(new TokenLoginFilter(redisTemplate,tokenManager,authenticationManager()))//添加登录过滤器
                .and().addFilter(new TokenAuthFilter(authenticationManager(),tokenManager,redisTemplate)).httpBasic();//添加权限过滤器
    }

    //配置访问数据库处理和加密格式
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }

    //配置不拦截路径
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().mvcMatchers("/user/**","/swagger**","/swagger-resources/**","/webjars/springfox-swagger-ui/**",
                "/yuhCloud/**"
                ,"/*.html","/","login.html"
                ,"login.js","signUp.js","sign-up.html",
                "/swagger-ui.html",
                "/v2/api-docs", // swagger api json
                "/swagger-resources/configuration/ui", // 用来获取支持的动作
                "/swagger-resources", // 用来获取api-docs的URI
                "/swagger-resources/configuration/security", // 安全选项
                "/swagger-resources/**",
                //补充路径，近期在搭建swagger接口文档时，通过浏览器控制台发现该/webjars路径下的文件被拦截，故加上此过滤条件即可。(2020-10-23)
                "/webjars/**");
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}
